package com.itheima.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.system.Permission;
import com.itheima.service.impl.system.PermissionServiceImpl;
import com.itheima.service.system.PermissionService;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/permission/*")
public class PermissionServlet extends BaseServlet {
    private PermissionService service = new PermissionServiceImpl();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 3;
        if (req.getParameter("page") != null)
            pageNum = Integer.parseInt(req.getParameter("page"));
        if (req.getParameter("pageSize") != null)
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        List<Permission> list =service.findByPage(pageNum,pageSize);
        //req.setAttribute("permissionList",list);
        PageInfo pageInfo = new PageInfo(list);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-list.jsp").forward(req,resp);
    }

    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-add.jsp").forward(req,resp);
    }

    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //List<Permission> list =service.findByPage();
        Permission permission = BeanUtil.fillBean(req, Permission.class);
        service.save(permission);
        //System.out.println(permission);
        resp.sendRedirect(req.getContextPath() + "/system/permission/list");
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //List<Permission> list =service.findByPage();

        String deleteIds = req.getParameter("ids");
        //System.out.println(deleteIds);
        service.delete(deleteIds);
        resp.sendRedirect(req.getContextPath() + "/system/permission/list");
    }


    protected void deletePermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        //System.out.println(deleteIds);
        service.deletePermission(id);
        resp.sendRedirect(req.getContextPath() + "/system/permission/list");
    }

    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Permission permission = service.findById(id);
        req.setAttribute("permission",permission);
        req.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-show.jsp").forward(req,resp);
    }
}
